package com.quectel.cms.controller.scheduling;


import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.cms.controller.BaseController;
import com.quectel.cms.vo.scheduling.SchedulingAuditPipelineSeqPersonsVo;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.dept.dto.DeptPersonDto;
import com.quectel.core.module.dept.service.DeptPersonService;
import com.quectel.core.module.scheduling.dto.SchedulingAuditPipelineDto;
import com.quectel.core.module.scheduling.dto.SchedulingAuditPipelineSeqDeptPersonDto;
import com.quectel.core.module.scheduling.service.SchedulingAuditPipelineSeqDeptPersonService;
import com.quectel.core.module.scheduling.service.SchedulingAuditPipelineService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/07/05 13:24
 */
@RestController
@RequestMapping("schedulingAuditPipeline")
@Tag(name = "SchedulingAuditPipelineController", description = "考勤审批流步骤配置相关api")
public class SchedulingAuditPipelineController extends BaseController {

    @DubboReference
    private SchedulingAuditPipelineService schedulingAuditPipelineService;
    @DubboReference
    private SchedulingAuditPipelineSeqDeptPersonService auditPipelineSeqDeptPersonService;
    @DubboReference
    private DeptPersonService deptPersonService;

    /**
     * 获取选择部门具体审批人穿梭框数据
     */
    @GetMapping("getDeptPersonTransfer")
    @RequirePermissions(values = "schedulingAuditPipeline:edit")
    @Operation(summary = "获取选择部门具体审批人穿梭框数据")
    public Response<SchedulingAuditPipelineSeqPersonsVo> getDeptPersonTransfer(
            @Parameter(description = "小区Id") @RequestParam Long villageId,
            @Parameter(description = "部门Id") @RequestParam Long deptId,
            @Parameter(description = "审批步骤Id") @RequestParam(required = false) Long auditPipelineSeqId
    ) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        SchedulingAuditPipelineSeqPersonsVo result = new SchedulingAuditPipelineSeqPersonsVo();

        //查询部门人员列表
        params.put(LambdaUtil.getFieldName(DeptPersonDto::getVillageId), villageId);
        params.put(LambdaUtil.getFieldName(DeptPersonDto::getDeptId), deptId);
        List<DeptPersonDto> deptPersonDtos = deptPersonService.queryList(params);

        List<SchedulingAuditPipelineSeqPersonsVo.TransferItem> typeTransferData = deptPersonDtos.stream().map(o -> {
            SchedulingAuditPipelineSeqPersonsVo.TransferItem transferItem = new SchedulingAuditPipelineSeqPersonsVo.TransferItem();
            transferItem.setKey(o.getId());
            transferItem.setLabel(o.getName());
            return transferItem;
        }).collect(Collectors.toList());
        result.setCanSelectDeptPersons(typeTransferData);
        result.setBindingDeptPersons(new Long[0]);
        if (auditPipelineSeqId != null) {
            params.put(LambdaUtil.getFieldName(SchedulingAuditPipelineSeqDeptPersonDto::getAuditPipelineSeqId), auditPipelineSeqId);
            List<SchedulingAuditPipelineSeqDeptPersonDto> auditPipelineSeqDeptPersonDtos = auditPipelineSeqDeptPersonService.queryList(params);
            if (CollectionUtils.isNotEmpty(auditPipelineSeqDeptPersonDtos)) {
                Long[] bindingDeptPersonIds = auditPipelineSeqDeptPersonDtos.stream().map(o -> o.getDeptPersonId()).toArray(Long[]::new);
                result.setBindingDeptPersons(bindingDeptPersonIds);
            }
        }
        return Response.<SchedulingAuditPipelineSeqPersonsVo>ok().wrap(result);
    }

    /**
     * 分页查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    @RequirePermissions(values = "schedulingAuditPipeline:list")
    @Operation(summary = "分页获取考勤审批流步骤配置列表")
    public Response<Page<SchedulingAuditPipelineDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());
        int total = schedulingAuditPipelineService.queryTotal(request.getParams());

        List<SchedulingAuditPipelineDto> list = null;
        if (total > 0) {
            list = schedulingAuditPipelineService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<SchedulingAuditPipelineDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<SchedulingAuditPipelineDto>>ok().wrap(page);
    }

    /**
     * 编辑查询反显
     *
     * @param id
     * @return
     */
    @GetMapping("infoForEdit/{id}")
    @RequirePermissions(values = "schedulingAuditPipeline:edit")
    @Operation(summary = "在编辑时根据id查询考勤审批流步骤配置详情")
    public Response<SchedulingAuditPipelineDto> infoForEdit(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (schedulingAuditPipelineService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        SchedulingAuditPipelineDto schedulingAuditPipelineDto = schedulingAuditPipelineService.selectById(id);

        return Response.<SchedulingAuditPipelineDto>ok().wrap(schedulingAuditPipelineDto);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "schedulingAuditPipeline:info")
    @Operation(summary = "根据id查询考勤审批流步骤配置的详情")
    public Response<SchedulingAuditPipelineDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (schedulingAuditPipelineService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        SchedulingAuditPipelineDto schedulingAuditPipelineDto = schedulingAuditPipelineService.selectById(id);

        return Response.<SchedulingAuditPipelineDto>ok().wrap(schedulingAuditPipelineDto);
    }

    /**
     * 保存
     *
     * @param schedulingAuditPipelineDto
     * @return
     */
    @PostMapping("save")
    @RequirePermissions(values = "schedulingAuditPipeline:edit")
    @Operation(summary = "新增考勤审批流步骤配置")
    public Response<Object> save(@RequestBody @Validated SchedulingAuditPipelineDto schedulingAuditPipelineDto) {

        //填充基础数据
        fillSaveCommonData(schedulingAuditPipelineDto);

        schedulingAuditPipelineService.save(schedulingAuditPipelineDto);

        return Response.ok();
    }


    /**
     * 修改
     *
     * @param schedulingAuditPipelineDto
     * @return
     */
    @PostMapping("update")
    @RequirePermissions(values = "schedulingAuditPipeline:edit")
    @Operation(summary = "根据id修改考勤审批流步骤配置")
    public Response<Object> update(@RequestBody  @Validated SchedulingAuditPipelineDto schedulingAuditPipelineDto) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(schedulingAuditPipelineDto.getId()));
        if (schedulingAuditPipelineService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        //填充基础数据
        fillEditCommonData(schedulingAuditPipelineDto);

        schedulingAuditPipelineService.updateById(schedulingAuditPipelineDto);

        return Response.ok();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("delete")
    @RequirePermissions(values = "schedulingAuditPipeline:delete")
    @Operation(summary = "根据多条记录id批量删除考勤审批流步骤配置")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (schedulingAuditPipelineService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        schedulingAuditPipelineService.deleteBatch(ids);

        return Response.ok();
    }
}
